﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package eu.orangeflash.webapis.xmlrpc {
    import eu.orangeflash.webapis.events.*;
    import flash.events.*;

    public class Client extends EventDispatcher implements IClient {

        protected var result:IMethodResponse;
        protected var url:String;

        public function Client(){
            super();
        }
        public function get lastResult():Object{
            return (((result.params.length)==1) ? result.params[0] : result.params);
        }
        protected function onFault(event:XMLRPCEvent):void{
            event.methodCall.removeEventListener(XMLRPCEvent.FAULT, onResult);
            dispatchEvent(new XMLRPCEvent(XMLRPCEvent.FAULT, this, event.methodCall));
            (event.target as IEventDispatcher).removeEventListener(event.type, onFault);
        }
        protected function onResult(event:XMLRPCEvent):void{
            event.methodCall.removeEventListener(XMLRPCEvent.RESULT, onResult);
            result = event.methodCall.response;
            dispatchEvent(new XMLRPCEvent(XMLRPCEvent.RESULT, this, event.methodCall));
            (event.target as IEventDispatcher).removeEventListener(event.type, onResult);
        }
        public function set serviceURL(value:String):void{
            url = value;
        }
        public function invoke(methodName:String, ... _args):IMethodCall{
            var result:MethodCall;
            result = new MethodCall(this);
            result.addEventListener(XMLRPCEvent.RESULT, onResult);
            result.addEventListener(XMLRPCEvent.FAULT, onFault);
            result.invoke(methodName, _args);
            return (result);
        }
        public function get serviceURL():String{
            return (url);
        }

    }
}//package eu.orangeflash.webapis.xmlrpc 
